package org.vehicle.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.vehicle.entity.DriverInfo;
import org.vehicle.entity.security.MyUserDetail;
import org.vehicle.service.DriverInfoService;


@Controller
public class DriverInfoController {
	private static Logger logger = LogManager.getLogger(DriverInfoController.class.getName());
	@Autowired
	private DriverInfoService driverInfoService;
	
	@RequestMapping("driverInfo")
	public String blacklistInfo(HttpServletRequest req, Model model){
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		model.addAttribute("manager", ud);
		return "driverInfo";
	}
	/**
	 * 获取司机信息分页
	 * @param request
	 * @return
	 */
	@RequestMapping("/getDriverInfoPage")
    @ResponseBody
    public Map<String, Object> getDriverInfoPage(HttpServletRequest request) {
    	Map<String, Object> map = new HashMap<String, Object>();
        String currentPage = request.getParameter("curr");
        String pageSize = request.getParameter("pageSize");
        String driverName = request.getParameter("driverName");
        String certificateCode = request.getParameter("certificateCode");
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startPage", (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize));
            paramMap.put("pageSize", Integer.parseInt(pageSize));
            paramMap.put("driverName", driverName);
            paramMap.put("certificateCode", certificateCode);
            map = driverInfoService.getDriverInfoPage(paramMap);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return map;
    }
	
	/**
	 * 获取司机信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/getDriverInfo")
    @ResponseBody
    public Map<String, Object> getDriverInfo(HttpServletRequest request) {
    	Map<String, Object> map = new HashMap<String, Object>();
    	String driverInfoId=request.getParameter("driverInfoId");
    	DriverInfo driverInfo=driverInfoService.selectByPrimaryKey(Long.parseLong(driverInfoId));
    	map.put("driverInfo", driverInfo);
        return map;
    }
	/**
	 * 添加司机信息
	 * @param req
	 * @return
	 */
	@RequestMapping("/addDriverInfo")
	@ResponseBody
	public Map<String, Object> addDriverInfo(DriverInfo driverInfo){
		Map<String, Object> paramMap=new HashMap<String, Object>();
		try{
			Map<String, Object> paramMap2=new HashMap<String, Object>();
			paramMap2.put("certificateCode", driverInfo.getCertificateCode());
			int count=driverInfoService.testDriverInfoByCertificateCode(paramMap2);
			if(count>0){
				paramMap.put("isSuccess", 0);
				paramMap.put("errMsg", "添加失败，身份证已经存在");
			}else{
				driverInfo.setCreateDate(new Date());
				driverInfo.setState(1);
				driverInfoService.insertSelective(driverInfo);
				paramMap.put("isSuccess", 1);
				paramMap.put("errMsg", null);
			}
		}catch(Exception e){
			logger.error("添加司机异常", e);
			paramMap.put("isSuccess", 0);
			paramMap.put("errMsg", "添加失败");
		}
		return paramMap;
	}
	
	/**
	 * 修改司机信息
	 * @param req
	 * @return
	 */
	@RequestMapping("/updateDriverInfo")
	@ResponseBody
	public Map<String, Object> updateDriverInfo(DriverInfo driverInfo){
		Map<String, Object> paramMap=new HashMap<String, Object>();
		try{
			Map<String, Object> paramMap2=new HashMap<String, Object>();
			paramMap2.put("certificateCode", driverInfo.getCertificateCode());
			paramMap2.put("driverInfoId", driverInfo.getDriverInfoId());
			int count=driverInfoService.testDriverInfoByCertificateCode(paramMap2);
			if(count>0){
				paramMap.put("isSuccess", 0);
				paramMap.put("errMsg", "修改失败，身份证已经存在");
			}else{
				driverInfoService.updateByPrimaryKeySelective(driverInfo);
				paramMap.put("isSuccess", 1);
				paramMap.put("errMsg", null);
			}
		}catch(Exception e){
			logger.error("修改司机信息异常", e);
			paramMap.put("isSuccess", 0);
			paramMap.put("errMsg", "修改失败");
		}
		return paramMap;
	}
	
	/**
	 * 删除司机
	 * @param req
	 * @return
	 */
	@RequestMapping("/delDriverInfo")
	@ResponseBody
	public Map<String, Object> delDriverInfo(DriverInfo driverInfo){
		Map<String, Object> paramMap=new HashMap<String, Object>();
		try{
			driverInfoService.deleteByPrimaryKey(driverInfo.getDriverInfoId());
			paramMap.put("isSuccess", 1);
			paramMap.put("errMsg", null);
		}catch(Exception e){
			logger.error("删除司机异常", e);
			paramMap.put("isSuccess", 0);
			paramMap.put("errMsg", "删除失败");
		}
		return paramMap;
	}
}
